package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.project.system.domain.SysMobileUser;
import com.ruoyi.project.system.domain.SysUserMoney;
import com.ruoyi.project.system.service.ISysMobileUserService;
import com.ruoyi.project.system.service.ISysUserMoneyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysUserWithdrawal2;
import com.ruoyi.project.system.service.ISysUserWithdrawal2Service;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 用户申请提现Controller
 *
 * @author jjp
 * @date 2020-04-23
 */
@RestController
@Api("用户申请提现Controller")
@RequestMapping("/system/withdrawal2")
public class SysUserWithdrawal2Controller extends BaseController {
    @Autowired
    private ISysUserWithdrawal2Service sysUserWithdrawal2Service;
    @Autowired
    private ISysUserMoneyService sysUserMoneyService;
    @Autowired
    private ISysMobileUserService sysMobileUserService;

    /**
     * 查询用户申请提现列表
     */
    @ApiOperation("查询用户申请提现列表")
    @PreAuthorize("@ss.hasPermi('system:withdrawal2:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUserWithdrawal2 sysUserWithdrawal2) {
        startPage();
        List<SysUserWithdrawal2> list = sysUserWithdrawal2Service.selectSysUserWithdrawal2List(sysUserWithdrawal2);
        return getDataTable(list);
    }


    /**
     * 获取用户申请提现详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:withdrawal2:query')")
    @ApiOperation("获取用户申请提现详细信息")
    @GetMapping(value = "/{withdrawalId}")
    public AjaxResult getInfo(@PathVariable("withdrawalId") Long withdrawalId) {
        return AjaxResult.success(sysUserWithdrawal2Service.selectSysUserWithdrawal2ById(withdrawalId));
    }

    /**
     * 新增用户申请提现
     */
    @PreAuthorize("@ss.hasPermi('system:withdrawal2:add')")
    @Log(title = "用户申请提现", businessType = BusinessType.INSERT)
    @ApiOperation("新增用户申请提现")
    @PostMapping
    public AjaxResult add(@RequestBody SysUserWithdrawal2 sysUserWithdrawal2) {
        return toAjax(sysUserWithdrawal2Service.insertSysUserWithdrawal2(sysUserWithdrawal2));
    }

    /**
     * 修改用户申请提现
     */
    @PreAuthorize("@ss.hasPermi('system:withdrawal2:edit')")
    @ApiOperation("修改用户申请提现")
    @Log(title = "用户申请提现", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserWithdrawal2 sysUserWithdrawal2) {
        int i = sysUserWithdrawal2Service.updateSysUserWithdrawal2(sysUserWithdrawal2);
        SysMobileUser sysMobileUser1 = sysMobileUserService.selectSysMobileUserById(sysUserWithdrawal2.getMobileUserId());
        SysMobileUser sysMobileUser = new SysMobileUser();
        sysMobileUser.setMoney(sysMobileUser1.getMoney() - sysUserWithdrawal2.getMoney());
        sysMobileUser.setMobileUserId(sysUserWithdrawal2.getMobileUserId());
        SysUserMoney sysUserMoney = new SysUserMoney();
        sysUserMoney.setMobileUserId(sysUserWithdrawal2.getMobileUserId());
        sysUserMoney.setMoney(sysUserWithdrawal2.getFactMoney());
        sysUserMoney.setMoneyType("1");
        sysUserMoney.setWithdrawalId(sysUserWithdrawal2.getWithdrawalId());
        if (i == 1) {
            sysUserMoneyService.insertSysUserMoney(sysUserMoney);
            sysMobileUserService.updateSysMobileUser(sysMobileUser);
            return AjaxResult.success();
        }
        return AjaxResult.success();
    }

    /**
     * 删除用户申请提现
     */
    @PreAuthorize("@ss.hasPermi('system:withdrawal2:remove')")
    @ApiOperation("删除用户申请提现")
    @Log(title = "用户申请提现", businessType = BusinessType.DELETE)
    @DeleteMapping("/{withdrawalIds}")
    public AjaxResult remove(@PathVariable Long[] withdrawalIds) {
        return toAjax(sysUserWithdrawal2Service.deleteSysUserWithdrawal2ByIds(withdrawalIds));
    }
}
